package com.glowworms.core.web.bind;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.NumberUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;

import com.glowworms.core.util.ObjectUtils;
import com.glowworms.core.util.WebUtils;

/**
 * Controller控制方法核心参数 主要新增一些工具类
 */
public class RequestAndResponse extends ServletWebRequest {

	/**
	 * 
	 * @param request
	 * @param response
	 */
	public RequestAndResponse(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
	}

	/**
	 * 没有则返回null
	 * 
	 * @param key
	 * @return
	 */
	public String getString(String key) {
		return getString(key, null);
	}

	/**
	 * 没有则返回默认值
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public String getString(String key, String defaultValue) {
		String r = getParameter(key);
		return ObjectUtils.isNull(r) ? defaultValue : r;
	}

	/**
	 * 没有返回null
	 * 
	 * @param key
	 * @return
	 */
	public Integer getInt(String key) {
		return getInt(key, null);
	}

	/**
	 * 没有返回null, 返回默认值
	 * 
	 * @param key
	 * @return
	 */
	public Integer getInt(String key, Integer defaultValue) {
		String r = getParameter(key);
		return ObjectUtils.isNull(r) ? NumberUtils.parseNumber(r, Integer.class) : defaultValue;
	}

	/**
	 * 没有返回null
	 * 
	 * @param key
	 * @return
	 */
	public Float getFloat(String key) {
		return getFloat(key, null);
	}

	/**
	 * 没有返回null, 返回默认值
	 * 
	 * @param key
	 * @return
	 */
	public Float getFloat(String key, Integer defaultValue) {
		String r = getParameter(key);
		return ObjectUtils.isNull(r) ? NumberUtils.parseNumber(r, Float.class) : defaultValue;
	}

	/**
	 * 是否文件上传
	 * 
	 * @return
	 */
	public boolean isMultipartRequest() {
		return getRequest() instanceof MultipartHttpServletRequest;
	}

	/**
	 * 获取上传请求
	 * 
	 * @return
	 */
	public MultipartHttpServletRequest getMultipartHttpServletRequest() {
		return (MultipartHttpServletRequest) getRequest();
	}

	/**
	 * 获取上传对象
	 * 
	 * @param key
	 * @return
	 */
	public UploadFile getFile(String key) {
		UploadFile file = null;
		if (isMultipartRequest()) {
			MultipartFile multipartFile = getMultipartHttpServletRequest().getFile(key);
			if (ObjectUtils.isNotNull(multipartFile)) {
				file = new UploadFile(multipartFile);
			}
		}
		return file;
	}
	
	/**
	 * 获取容器上下文
	 * @return
	 */
	public WebApplicationContext getWebApplicationContext(){
		return (WebApplicationContext) getRequest().getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	}
	
	/**
	 * 是否是ajax请求
	 * @return
	 */
	public boolean isAjaxRequest(){
		return WebUtils.isAjaxRequest(getRequest());
	}
	
	/**
	 * 返回JSON视图
	 * @return
	 */
	public ModelAndView toJSONView(){
		return null;
	};
	
	/**
	 * 返回模板视图
	 * @return
	 */
	public ModelAndView toTemplateView(){
		return null;
	}
}
